package com.pxmeta.party.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pxmeta.common.constant.Status;
import com.pxmeta.party.entity.ActivityFoodStall;
import com.pxmeta.party.entity.ActivityFoodStallUserRecord;
import com.pxmeta.party.entity.Gift;
import com.pxmeta.party.entity.vo.ActivityFoodSuccessVO;
import com.pxmeta.party.entity.vo.ActivityFoodVO;
import com.pxmeta.party.mapper.ActivityFoodStallMapper;
import com.pxmeta.party.mapper.ActivityFoodStallUserRecordMapper;
import com.pxmeta.party.mapper.GiftMapper;
import com.pxmeta.party.service.IActivityFoodStallService;
import com.pxmeta.party.service.IActivityFoodStallUserRecordService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ActivityFoodStallUserRecordServiceImpl extends ServiceImpl<ActivityFoodStallUserRecordMapper, ActivityFoodStallUserRecord> implements IActivityFoodStallUserRecordService {
    private final UserMapper userMapper;
    private final GiftMapper giftMapper;
    private final ActivityFoodStallMapper activityFoodStallMapper;
    @Override
    public List<ActivityFoodSuccessVO> getSuccessList(Integer page,Integer size, Integer number) {
        LambdaQueryWrapper<ActivityFoodStallUserRecord> wrapper = Wrappers.lambdaQuery();
        if (number !=null){
            wrapper.eq(ActivityFoodStallUserRecord::getActivityId,number);
        }
        wrapper.eq(ActivityFoodStallUserRecord::getStatus, Status.SUCCESS.getValue())
                .orderByDesc(ActivityFoodStallUserRecord::getCreateTime);

        ActivityFoodStall activityFoodStall = activityFoodStallMapper.selectById(number);
        List<ActivityFoodStallUserRecord> records = this.page(new Page<>(page, size), wrapper).getRecords();
        List<ActivityFoodSuccessVO> finalList = Lists.newLinkedList();
        for (ActivityFoodStallUserRecord record : records) {
            ActivityFoodSuccessVO vo = new ActivityFoodSuccessVO();
            vo.setNumber(record.getActivityId());
            User user = userMapper.selectById(record.getUserId());
            Gift gift = giftMapper.selectById(activityFoodStall.getGiftId());
            vo.setAvatar(user.getAvatar());
            vo.setNickname(user.getNickname());
            vo.setGiftIcon(gift.getIcon());
            vo.setGiftName(gift.getName());
            finalList.add(vo);
        }
        return finalList;
    }
}
